package com.huxiu;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huxiu.utils.Constants;
import com.huxiu.utils.FileUtils;
import com.huxiu.utils.Global;
import com.huxiu.utils.LogUtil;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.UmengUpdate;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

import java.io.File;
public class SettingActivity extends BaseActivity implements OnClickListener {
    @ViewInject(R.id.bangding)
    private LinearLayout bangDing;

    @ViewInject(R.id.clear_cache)
    private LinearLayout clearCache;

    @ViewInject(R.id.cache_size)
    private TextView cacheSize;

//应用评分先不用做
//    @ViewInject(R.id.pingfen)
//    private LinearLayout pingfen;

    @ViewInject(R.id.about_us)
    private LinearLayout aboutUs;

    @ViewInject(R.id.log_out)
    private TextView log_out;

    @ViewInject(R.id.back)
    private ImageView back;

    @ViewInject(R.id.layout_update)
    private LinearLayout layout_update;

    @ViewInject(R.id.text_isupdate)
    private TextView text_isupdate;

    private Intent intent;

    private SettingActivity instance;

    private UmengUpdate mUmengUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        instance=this;
        ViewUtils.inject(this);
        bangDing.setOnClickListener(this);
        clearCache.setOnClickListener(this);
//        pingfen.setOnClickListener(this);
        aboutUs.setOnClickListener(this);
        log_out.setOnClickListener(this);
        back.setOnClickListener(this);
        layout_update.setOnClickListener(this);
        getCacheSize();
        initData();
        if (!Settings.isLoggedIn()) {
            log_out.setVisibility(View.GONE);
        }
        mUmengUpdate=new UmengUpdate(instance,text_isupdate,false);
        UmengUpdateAgent.setUpdateAutoPopup(false);
        UmengUpdateAgent.setUpdateListener(mUmengUpdate);
        UmengUpdateAgent.update(this);
    }




    private void getCacheSize() {
        File f = FileUtils.getCacheDir();
        long size = FileUtils.folderSize(f);
        if (size < 60000) {
            size = 0;
        }
        cacheSize.setText(FileUtils.getFileSizeString(size));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bangding:
                if (!Settings.isLoggedIn()) {
                    Utils.showToast("登录后才能绑定");
                    Utils.askLogIn(this);
                    return;
                }
                UmenEvent.UmengEventMap(instance, UmenEvent.APP_USERCENTER_SETTING, UmenEvent.SETTING_ACCOUNT_BIND);
                Intent intent = new Intent(this, BindAccountsActivity.class);
                startActivity(intent);
                break;
            case R.id.clear_cache:
                clearCache();
                break;
//            case R.id.pingfen:
//                pingFen();
//                break;
            case R.id.about_us:
                UmenEvent.UmengEventMap(instance, UmenEvent.APP_USERCENTER_SETTING, UmenEvent.ABOUT_US);
                goAbout();
                break;
            case R.id.log_out:
                logOut();
                break;
            case R.id.back:
                finish();
                break;
            case R.id.layout_update:

                UmenEvent.UmengEventMap(instance, UmenEvent.APP_USERCENTER_SETTING, UmenEvent.SETTING_UPDATA);
                mUmengUpdate.setDialogopen(true);
//                UmengUpdateAgent.setUpdateAutoPopup(true);
                UmengUpdateAgent.forceUpdate(this);
                break;
        }
    }

    private void logOut() {
        new AlertDialog.Builder(this).setMessage(getString(R.string.confirm_log_out))
                .setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Settings.logOut();
                        Global.user = null;
                        //退出账号时清空绑定信息
                        Settings.clearSinaLogInInfo();
                        Settings.clearQQLogInInfo();
                        Settings.saveWeixinUid("");
                        UmenEvent.UmengEventMap(instance, UmenEvent.APP_USERCENTER_SETTING, UmenEvent.SETTING_LOGOUT);
                        LocalBroadcastManager.getInstance(APP.getInstance())
                                .sendBroadcast(new Intent(Constants.INTENT_USER_LOGGED_OUT));
                        finish();
                    }
                }).setNeutralButton(getString(android.R.string.cancel), null).show();
    }

    private void goAbout() {
        intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

//    private void pingFen() {
//        Uri uri = Uri.parse("market://details?id=" + getPackageName());
//        intent = new Intent(Intent.ACTION_VIEW, uri);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        startActivity(intent);
//    }

    private void clearCache() {
        new AlertDialog.Builder(this)
                .setMessage(getString(R.string.ask_clear_cache))
                .setPositiveButton(getString(android.R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                ImageLoader.getInstance().clearDiskCache();
                                getCacheSize();
                                UmenEvent.UmengEventMap(instance, UmenEvent.APP_USERCENTER_SETTING, UmenEvent.CLEAR_CACHE);
                                Utils.showToast(getString(R.string.clear_cache_done));
                            }
                        }
                ).setNeutralButton(getString(android.R.string.cancel), null)
                .show();

    }

    public void initData() {
        //注册广播接收者，并添加多个过滤器
        IntentFilter intent = new IntentFilter();
        intent.addAction(Constants.INTENT_USER_LOGGED_OUT);
        intent.addAction(Constants.INTENT_USER_LOGGED_IN);
        LocalBroadcastManager.getInstance(APP.getInstance()).registerReceiver(mReceiver, intent);
    }


    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constants.INTENT_USER_LOGGED_IN)) {
                log_out.setVisibility(View.VISIBLE);
            } else if (action.equals(Constants.INTENT_USER_LOGGED_OUT)) {
                log_out.setVisibility(View.GONE);
            }

        }
    };



    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
